import threading
import time
import logging

from tools import config
from tools import ringbuffer
from plugins.linux import uptime
from plugins.linux import loadavg

logging.basicConfig (level = logging.DEBUG, format = "%(created)-15s %(msecs)d %(levelname)8s %(thread)d %(name)s %(message)s")
log = logging.getLogger (__name__)


class CollThread(threading.Thread):
	""" """
	def __init__ (self, plugins_and_rb):
		""" """
		self.debug = config.getDebug ()
		if self.debug :
			log.debug ("__init__")
		threading.Thread.__init__ (self)
		self.pl_and_rb = plugins_and_rb
		self.counters = [[],[]]

	def run(self):
		"""infinite loop for collecting data"""
		for plugin in self.pl_and_rb[0] :
			self.counters[0].append (0)
			self.counters[1].append (0)
		while 1:
			for plugin in self.pl_and_rb[0]:
				idx=self.pl_and_rb[0].index(plugin)
				self.counters[0][idx] += 1
				if self.counters[0][idx] == self.pl_and_rb[2][idx] :
					self.pl_and_rb[3][idx].acquire()
					self.pl_and_rb[1][idx].append ([str \
						(self.counters[1][idx]), eval (plugin + ".get()")])
					self.pl_and_rb[3][idx].notifyAll ()
					self.pl_and_rb[3][idx].release ()
					self.counters[1][idx] += 1
					self.counters[0][idx] = 0
			time.sleep (1)
